package commons.ibatis;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class BaseDao {

    public static SqlSessionFactory getFactory() {

        try {
            String resource = "mybatis-config.xml";
            InputStream is = Resources.getResourceAsStream(resource);
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
            return factory;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }

    }

    public static SqlSession getSession() {
        SqlSessionFactory factory = getFactory();
        return factory.openSession();
    }

    public static SqlSession getSession(boolean isAutuCommit) {
        SqlSessionFactory factory = getFactory();
        return factory.openSession(isAutuCommit);
    }
}
